package com.ph.shopping.facade.score.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.customenum.TransCodeEnum;
import com.ph.shopping.facade.score.service.IScoreService;
import com.ph.shopping.facade.score.vo.MemberCostScore;
import com.ph.shopping.facade.score.vo.MemberScoreVo1;

@Component
public class ScoreScheduler extends BaseService {

	@Autowired
	private IScoreService iScoreService;

	// 返回可用积分每天凌晨1点执行 测试为每5分钟执行一次
	//@Scheduled(cron = "0 0 1 * * ?")
	@Scheduled(cron = "0 */5 * * * ?")
	public void returnEnableScore() {
		List<MemberScoreVo1> memberScores = iScoreService.getMemberScores();
		if (memberScores != null && memberScores.size() > 0) {
			for (MemberScoreVo1 vo1 : memberScores) {
				try {
					if (vo1.getStandbyscore() > 0) {
						iScoreService.memberScoreTrade(vo1.getMemberId(),
								TransCodeEnum.MEMBER_COST_STANDBYSCORE, vo1.getStandbyscore(), "");
					}
				} catch (Exception e) {
					logger.error(vo1.getMemberId() + ": 返回积分失败!");
					e.printStackTrace();
				}
			}
		} else {
			logger.info("暂无会员需返回可用积分!");
		}
	}

	// 返回待用积分(每隔7天执行) 测试为每5分钟执行一次
//	@Scheduled(cron = "0 0 1 */7 * ?")
	@Scheduled(cron = "0 */1 * * * ?")
	public void returnStnadByScore() {
		List<MemberCostScore> list = iScoreService.getMemberCostScore();
		if (list == null || list.size() == 0) {
			logger.info("暂无会员需返回待用积分!");
			return;
		}
		try {
			for (MemberCostScore costScore : list) {
				iScoreService.memberScoreTrade(costScore.getMemberId(),
						TransCodeEnum.MEMBER_BACK_SCORE, costScore.getPayMoney(),
						costScore.getOrderNo());
				logger.info("会员返回待用积分成功!");
			}
		} catch (Exception e) {
			logger.error("返回待用积分失败!");
			e.printStackTrace();
		}
	}

}
